package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ApiController {

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private ApiService apiService;

    @Autowired
    private WmNewsService wmNewsService;




    /**
     * 远程调用添加自媒体账号
     * @param wmUser
     * @return
     */
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }


    /**
     * 通appUserId查询自媒体账号信息
     * @param appUserId
     * @return
     */
    @GetMapping("/api/wmUser/{appUserId}")
    public ResultVo<WmUser> getByAppUserId(@PathVariable(name = "appUserId") Integer appUserId){
        WmUser wmUser = wmUserService.getByAppUserId(appUserId);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.save(wmMaterial);
        return ResultVo.ok();
    }

    /**
     * 查询频道列表
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> getChannelList(){
        List<AdChannel> list = apiService.getChannelList();
        return ResultVo.ok(list);
    }

    /**
     * 通过文章id查询信息
     * @param newsId
     * @return
     */
    @GetMapping("/api/wmNews/{newsId}")
    public ResultVo<WmNews> getWmNewsById(@PathVariable(name = "newsId") Long newsId){
        WmNews news = wmNewsService.getById(newsId);
        return ResultVo.ok(news);
    }


    /**
     * 更新文章状态
     * @param wmNews
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo<WmNews> update(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok();
    }

    /**
     * 给后台管理用的文章分页查询，4是for的缩写
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo<WmNewsVo> findPage4Admin(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNewsVo> pageResultVo = wmNewsService.findPage4Admin(dto);
        return pageResultVo;
    }

    /**
     * 人工审核-驳回
     * @param paramMap
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody Map<String,Object> paramMap){
        wmNewsService.authFail(paramMap);
        return ResultVo.ok();
    }

    /**
     * 人工审核-通过
     * @param paramMap
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody Map<String,Long> paramMap){
        wmNewsService.authPass(paramMap);
        return ResultVo.ok();
    }

    /**
     * 人工审核-查看文章详情
     * @param newsId
     * @return
     */
    @GetMapping("/news/one_vo/{newsId}")
    public ResultVo<WmNewsVo> getWmNewsVo(@PathVariable(name = "newsId") Long newsId){
        WmNewsVo vo = wmNewsService.getWmNewsVo(newsId);
        return ResultVo.ok(vo);
    }

    /**
     * 查询需要同步的文章status=8
     * @param dto
     * @return
     */
    @PostMapping("/api/wmNews/list4ArticleSync")
    public ResultVo<List<WmNewsVo>> list4ArticleSync(@RequestBody WmNewsPageReqDto dto){
        List<WmNewsVo> list = wmNewsService.list4ArticleSync(dto);
        return ResultVo.ok(list);
    }
}
